package com.blakequ.androidblemanager.ui;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.blakequ.androidblemanager.R;
import com.blakequ.androidblemanager.adapter.DeviceListAdapter;
import com.blakequ.androidblemanager.containers.BluetoothLeDeviceStore;
import com.blakequ.androidblemanager.event.UpdateEvent;
import com.blakequ.androidblemanager.service.MyService;
import com.blakequ.androidblemanager.utils.BLEUtil;
import com.blakequ.androidblemanager.utils.BluetoothUtils;
import com.blakequ.androidblemanager.utils.Constants;
import com.blakequ.androidblemanager.utils.IntentUtils;
import com.blakequ.androidblemanager.utils.LocationUtils;
import com.blakequ.androidblemanager.utils.PreferencesUtils;
import com.blakequ.androidblemanager.utils.ToastX;
import com.blakequ.androidblemanager.widget.MyAlertDialog;
import com.tamsiree.rxkit.RxActivityTool;
import com.tencent.mmkv.MMKV;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.PermissionUtils;

public class LoginPage extends BaseActivity {
    @BindView(R.id.id)
    protected EditText id;
    @BindView(R.id.login)
    protected Button login;


    private String[] permissionList = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    boolean isVisible = false;
    //    private BluetoothUtils mBluetoothUtils;
//    private BluetoothLeDeviceStore mDeviceStore;
    boolean isInited = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);
        ButterKnife.bind(this);
        String userId = MMKV.defaultMMKV().getString("id",null);
        if (!TextUtils.isEmpty(userId)) {
            Intent intent = new Intent(LoginPage.this, MainPage.class);
            startActivity(intent);
            finish();
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Utils.isPermissionOpen(LoginPage.this)) {
                    AlertDialog dialog = new AlertDialog.Builder(LoginPage.this)
                            .setPositiveButton("去授权", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Utils.openPermissionSetting(LoginPage.this);
                                }
                            })
                            .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                    finish();
                                    RxActivityTool.AppExit(LoginPage.this);
                                }
                            })
                            .setCancelable(false)
                            .setMessage("为了正常运行，需要授权通知栏权限")
                            .create();
                    dialog.show();
                } else if (!checkPermission()) {

                } else {
                    String id = LoginPage.this.id.getText().toString().trim();
                    if (TextUtils.isEmpty(id)) {
                        ToastX.show("请输入您的用户ID");
                    } else {
                        MMKV.defaultMMKV().putString("id", id);
                        Intent intent = new Intent(LoginPage.this, MainPage.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });

    }


    private BluetoothUtils getUtil() {
        return BLEUtil.getInstance().getmBluetoothUtils();
    }


    @Override
    public void onResume() {
        super.onResume();

        isVisible = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isVisible = false;
    }


}
